*** Stata 15 reproduction code for

*Mader, Matthias & Harald Schoen.
*What a difference an event can make: The European refugee crisis, party competition, and voters’ responses in Germany
*West European Politics


*** Figure 3: Effects of change in absolute distance to CDU on approval change
**********************************************************************************

use "DATA_wide.dta", clear

*** Left panel of Figure 3
	matrix w6w11 = J(6,3,.) // 5 rows, 3 columns 
 	matrix w6w13 = J(6,3,.) // 5 rows, 3 columns 
 	matrix w6w16 = J(6,3,.) // 5 rows, 3 columns
				
	*CDU approval
 	foreach i in 11 13 16 {
	reg diff6_b_cdu`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	matrix list r(table)
	mat r=r(table)
	matrix w6w`i'[1, 1] = r[1,1]
	matrix w6w`i'[1, 2] = r[5,1]
	matrix w6w`i'[1, 3] = r[6,1]
	matrix list w6w`i'
	}
	*CSU approval
	foreach i in 11 13 16{
	reg diff6_b_csu`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	matrix list r(table)
	mat r=r(table)
	matrix w6w`i'[2, 1] = r[1,1]
	matrix w6w`i'[2, 2] = r[5,1]
	matrix w6w`i'[2, 3] = r[6,1]
	matrix list w6w`i'
	}
	*AfD approval
	foreach i in 11 13 16 {
	reg diff6_b_afd`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	matrix list r(table)
	mat r=r(table)
	matrix w6w`i'[3, 1] = r[1,1]
	matrix w6w`i'[3, 2] = r[5,1]
	matrix w6w`i'[3, 3] = r[6,1]
	matrix list w6w`i'
	}
	*Merkel approval	
	foreach i in 11 13 16 {
	reg diff6_merkel`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i'  rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	mat r=r(table)
	matrix w6w`i'[4, 1] = r[1,1]
	matrix w6w`i'[4, 2] = r[5,1]
	matrix w6w`i'[4, 3] = r[6,1]
	matrix list w6w`i'	
	}
	*Seehofer approval	
	foreach i in 11 13 16 {
	reg diff6_seeh`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i'  rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	mat r=r(table)
	matrix w6w`i'[5, 1] = r[1,1]
	matrix w6w`i'[5, 2] = r[5,1]
	matrix w6w`i'[5, 3] = r[6,1]
	matrix list w6w`i'	
	}
	*Petry approval	
	foreach i in 11 13 16 {
	reg diff6_petry`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i'  rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	mat r=r(table)
	matrix w6w`i'[6, 1] = r[1,1]
	matrix w6w`i'[6, 2] = r[5,1]
	matrix w6w`i'[6, 3] = r[6,1]
	matrix list w6w`i'	
	}

*** Create left panel of Figure 3
	grstyle init
	grstyle set imesh, horizontal 
	coefplot (matrix(w6w11[,1]), ci((w6w11[,2] w6w11[,3])) msymbol(circle) mcolor(gs6) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.16)) ///
			 (matrix(w6w13[,1]), ci((w6w13[,2] w6w13[,3])) msymbol(circle) mcolor(gs13) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.00)) ///
			 (matrix(w6w16[,1]), ci((w6w16[,2] w6w16[,3])) msymbol(circle) mcolor(gs16) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(-0.16)) ///
			, /// 
			coeflabels(r1="{&Delta} CDU approval" r2="{&Delta} CSU approval" r3="{&Delta} AfD approval" r4="{&Delta} Merkel approval" r5="{&Delta} Seehofer approval" r6="{&Delta} Petry approval" ,labsize(large) labcolor(gs0)) ///
			xscale(range(-.6 .6)) xlabel(-.6 (.2) .6, noticks labsize(large) labcolor(gs0)) xmtick(none) ///
			legend(order(2 "w3-w7" 4 "w3-w8" 6 "w3-w9") position(6) col(4)  size(large) symxsize(5) region(lcolor(gs16)))  /// 
			plotregion(color(gs15)) ///
			graphregion(margin(-5 0 -5 0)) ysize(2.5) xsize(3.5)  /// scale(.8)
			title("Effects of {&Delta} absolute distance to CDU on...", size(large) color(gs0)) ///
			name(fig3a, replace)

*** Right panel of Figure 3
			
	matrix w6w11 = J(6,3,.) // 5 rows, 3 columns 
 	matrix w6w13 = J(6,3,.) // 5 rows, 3 columns 
 	matrix w6w16 = J(6,3,.) // 5 rows, 3 columns
				
	*CDU approval
 	foreach i in 11 13 16 {
	reg diff6_b_cdu`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimates store m_diff6_b_cdu`i' // store regression results for table production (see below)
	matrix list r(table)
	mat r=r(table)
	matrix w6w`i'[1, 1] = r[1,2]
	matrix w6w`i'[1, 2] = r[5,2]
	matrix w6w`i'[1, 3] = r[6,2]
	matrix list w6w`i'
	}
	*CSU approval
	foreach i in 11 13 16{
	reg diff6_b_csu`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimates store m_diff6_b_csu`i'
	matrix list r(table)
	mat r=r(table)
	matrix w6w`i'[2, 1] = r[1,2]
	matrix w6w`i'[2, 2] = r[5,2]
	matrix w6w`i'[2, 3] = r[6,2]
	matrix list w6w`i'
	}
	*AfD approval
	foreach i in 11 13 16 {
	reg diff6_b_afd`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimates store m_diff6_b_afd`i'
	matrix list r(table)
	mat r=r(table)
	matrix w6w`i'[3, 1] = r[1,2]
	matrix w6w`i'[3, 2] = r[5,2]
	matrix w6w`i'[3, 3] = r[6,2]
	matrix list w6w`i'
	}
	*Merkel approval	
	foreach i in 11 13 16 {
	reg diff6_merkel`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimates store m_diff6_merkel`i'
	mat r=r(table)
	matrix w6w`i'[4, 1] = r[1,2]
	matrix w6w`i'[4, 2] = r[5,2]
	matrix w6w`i'[4, 3] = r[6,2]
	matrix list w6w`i'	
	}
	*Seehofer approval	
	foreach i in 11 13 16 {
	reg diff6_seeh`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimates store m_diff6_seeh`i'
	mat r=r(table)
	matrix w6w`i'[5, 1] = r[1,2]
	matrix w6w`i'[5, 2] = r[5,2]
	matrix w6w`i'[5, 3] = r[6,2]
	matrix list w6w`i'	
	}
	*Petry approval	
	foreach i in 11 13 16 {
	reg diff6_petry`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimates store m_diff6_petry`i'
	margins, dydx(rdiff6_adist_cdu`i' rdiff6_adist_csu`i')
	mat r=r(table)
	matrix w6w`i'[6, 1] = r[1,2]
	matrix w6w`i'[6, 2] = r[5,2]
	matrix w6w`i'[6, 3] = r[6,2]
	matrix list w6w`i'	
	}
	
*** Create right panel of Figure 3
	grstyle init
	grstyle set imesh, horizontal 
	coefplot (matrix(w6w11[,1]), ci((w6w11[,2] w6w11[,3])) msymbol(circle) mcolor(gs6) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.16)) ///
			 (matrix(w6w13[,1]), ci((w6w13[,2] w6w13[,3])) msymbol(circle) mcolor(gs13) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.00)) ///
			 (matrix(w6w16[,1]), ci((w6w16[,2] w6w16[,3])) msymbol(circle) mcolor(gs16) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(-0.16)) ///
			, /// 
			coeflabels(r1="{&Delta} CDU approval" r2="{&Delta} CSU approval" r3="{&Delta} AfD approval" r4="{&Delta} Merkel approval" r5="{&Delta} Seehofer approval" r6="{&Delta} Petry approval" ,labsize(large)  labcolor(gs0)) ///
			xscale(range(-.6 .6)) xlabel(-.6 (.2) .6, noticks labsize(large) labcolor(gs0)) xmtick(none) ///
			legend(order(2 "w3-w7" 4 "w3-w8" 6 "w3-w9") position(6) col(4)  size(large) symxsize(5) region(lcolor(gs16)))  /// 
			plotregion(color(gs15)) ///
			graphregion(margin(-5 0 -5 0)) ysize(2.5) xsize(3.5)  /// scale(.8)
			title("Effects of {&Delta} absolute distance to CSU on...", size(large)  color(gs0)) ///
			name(fig3b, replace)


*** Appendix: Regression tables for Figure 3
***********************************************

	foreach var in cdu csu afd {
	esttab m_diff6_b_`var'11 m_diff6_b_`var'13 m_diff6_b_`var'16 using Tables_Figure3_`var'.rtf, replace /// 
		stats(r2 N , fmt(%9.2g) labels("R2" "N")) ///
		cells(b(star fmt(2)) se(par fmt(2) )) ///
		mtitles("w3-w7" "w3-w8" "w3-w9") ///
		varlabels ///
			 (rdiff6_adist_cdu11 "Change in absolute immigration distance to the CDU (w3-w7)" ///
			 rdiff6_adist_csu11 "Change in absolute immigration distance to the CSU (w3-w7)" ///
			 rdiff6_adist_cdu13 "Change in absolute immigration distance to the CDU (w3-w8)" ///
			 rdiff6_adist_csu13 "Change in absolute immigration distance to the CSU (w3-w8)" ///
			 rdiff6_adist_cdu16 "Change in absolute immigration distance to the CDU (w3-w9)" ///
			 rdiff6_adist_csu16 "Change in absolute immigration distance to the CSU (w3-w9)" ///
			 rdiff6_wadist_cdu11 "Change in absolute welfare distance to the CDU (w3-w7)" ///
			 rdiff6_wadist_csu11 "Change in absolute welfare distance to the CSU (w3-w7)" ///
			 rdiff6_wadist_cdu13 "Change in absolute welfare distance to the CDU (w3-w8)" ///
			 rdiff6_wadist_csu13 "Change in absolute welfare distance to the CSU (w3-w8)" ///
			 rdiff6_wadist_cdu16 "Change in absolute welfare distance to the CDU (w3-w9)" ///
			 rdiff6_wadist_csu16 "Change in absolute welfare distance to the CSU (w3-w9)" ///
			 _cons "Constant") ///
		title("Table: Regression results for change in xxx approval") ///
		addnotes("Notes: Reported are OLS coefficients with standard errors in parentheses. *: p<0,05, **: p<0,01, ***: p<0,001.") /// 
		varwidth(40) modelwidth(15)	
	}
	
	foreach var in merkel seeh petry {
	esttab m_diff6_`var'11 m_diff6_`var'13 m_diff6_`var'16 using Tables_Figure3_`var'.rtf, replace /// 
		stats(r2 N , fmt(%9.2g) labels("R2" "N")) ///
		cells(b(star fmt(2)) se(par fmt(2) )) ///
		mtitles("w3-w7" "w3-w8" "w3-w9") ///
		varlabels ///
			 (rdiff6_adist_cdu11 "Change in absolute immigration distance to the CDU (w3-w7)" ///
			 rdiff6_adist_csu11 "Change in absolute immigration distance to the CSU (w3-w7)" ///
			 rdiff6_adist_cdu13 "Change in absolute immigration distance to the CDU (w3-w8)" ///
			 rdiff6_adist_csu13 "Change in absolute immigration distance to the CSU (w3-w8)" ///
			 rdiff6_adist_cdu16 "Change in absolute immigration distance to the CDU (w3-w9)" ///
			 rdiff6_adist_csu16 "Change in absolute immigration distance to the CSU (w3-w9)" ///
			 rdiff6_wadist_cdu11 "Change in absolute welfare distance to the CDU (w3-w7)" ///
			 rdiff6_wadist_csu11 "Change in absolute welfare distance to the CSU (w3-w7)" ///
			 rdiff6_wadist_cdu13 "Change in absolute welfare distance to the CDU (w3-w8)" ///
			 rdiff6_wadist_csu13 "Change in absolute welfare distance to the CSU (w3-w8)" ///
			 rdiff6_wadist_cdu16 "Change in absolute welfare distance to the CDU (w3-w9)" ///
			 rdiff6_wadist_csu16 "Change in absolute welfare distance to the CSU (w3-w9)" ///
			 _cons "Constant") ///
		title("Table: Regression results change in xxx approval") ///
		addnotes("Notes: Reported are OLS coefficients with standard errors in parentheses. *: p<0,05, **: p<0,01, ***: p<0,001.") /// 
		varwidth(40) modelwidth(15)	
	}
	